<?php
include './stuSQL.php';

header('Content-Type:application/json; charset=utf-8'); #返回json

$input = file_get_contents('php://input'); 
// echo $input;
$arr = json_decode($input, true);  //转化为普通数据

// print_r($arr['id']);


$stuSQL = new StuSql();
$stuSQL -> add($arr);



$dbms = 'mysql';     //数据库类型
$host = '127.0.0.1'; //数据库主机名
$dbName = 'g09short';    //使用的数据库
$user = 'root';      //数据库连接用户名
$pass = '123';          //对应的密码
$dsn = "$dbms:host=$host;dbname=$dbName";

/*
try {
    $dbh = new PDO($dsn, $user, $pass); //初始化一个PDO对象

    $stu_class = array_key_exists('stu_class',$arr)?$arr['stu_class']:'';
    $stu_college = array_key_exists('stu_college',$arr)?$arr['stu_college']:'';

    if (array_key_exists('isAdd',$arr) && $arr['isAdd']){                 //添加一条记录
        $sql = "SELECT * FROM zyh_student WHERE stu_id like '" . $arr['stu_id'] . "'";
        $stmt = $dbh->query($sql, PDO::FETCH_ASSOC);
        $isExsit = $stmt -> rowCount();         //查询是否已存在
        if ($isExsit == 0){
            $sql = "INSERT INTO zyh_student (`stu_id`, `stu_pwd`, `stu_name`, `stu_class`, `stu_college`) VALUE('" . $arr['stu_id'] . "','" . $arr['stu_id'] . "','" . $arr['stu_name'] . "','" . $stu_class . "','" . $stu_college . "')";
            $count = $dbh->exec($sql);
            if ($count > 0){
                $result = [
                    'code' => '0',
                    'msg' => '插入信息成功',
                ];
            }
            else{
                $result = [
                    'code' => '1',
                    'msg' => '插入信息失败',
                ];
            }
        }
        else{
            $result = [
                'code' => '1',
                'msg' => '学号已存在',
            ];
        }
    }
    else{
        $sql = "UPDATE zyh_student SET `stu_name` = '" . $arr['stu_name'] . "', `stu_class` = '" . $stu_class . "', `stu_college` = '" .  $stu_college  . "' WHERE stu_id = '" . $arr['stu_id'] . "'";
        $count = $dbh->exec($sql);
        if ($count == 0){
            $result = [
                'code' => '1',
                'msg' => '修改信息失败',
            ];
        }
        else{
            $result = [
                'code' => '0',
                'msg' => '修改信息成功',
            ];
        }
    }
    $dbh=null;

    //返回json
    echo json_encode($result);
    

} catch (PDOException $e) {
    die("Error!: " . $e->getMessage() . "<br/>");
}

*/
